package com.lv.service.impl;

import com.lv.mapper.TbItemCatMapper;
import com.lv.pojo.TbItemCat;
import com.lv.pojo.TbItemCatExample;
import com.lv.service.CatService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class CatServiceImpl implements CatService {
    @Resource
    private TbItemCatMapper cm;



    @Override
    public List<TbItemCat> selectParendId(Long parentId) {
        return cm.selectParendId(parentId);
    }

    @Override
    public TbItemCat selectID(Long id) {
        return cm.selectByPrimaryKey(id);
    }

    @Override
    public Integer add(TbItemCat c) {
        return cm.insertSelective(c);
    }

    @Override
    public Integer del(List<Long> ids) {
        for (Long id : ids) {
            TbItemCat tbItemCat = cm.selectByPrimaryKey(id);
            Long parentId = tbItemCat.getParentId();
            cm.deleteByPrimaryKey(id);
            if (id !=null && id!=0){
            cm.deleteByParentId(id);//根据parentId去删除他的下级
            }
        }
        return null;
    }

    @Override
    public TbItemCat toupdate(Long id) {
        return cm.selectByPrimaryKey(id);
    }

    @Override
    public TbItemCat updateId(Long id) {
        TbItemCat tbItemCat = cm.selectByPrimaryKey(id);
        Long Id = tbItemCat.getParentId();
        TbItemCat t = cm.selectByPrimaryKey(Id);
        return t;
    }

    @Override
    public Integer update(TbItemCat c) {
        return cm.updateByPrimaryKeySelective(c);
    }

    @Override
    public List<TbItemCat> getOne(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        return cm.selectByExample(example);

    }
}
